﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace AppSocketService.LinuxKafkaServerCheck
{
    public class LinuxKafkaServerCheck
    {
        /// <summary>
        ///  停止Kafka和Zookeeper服务
        /// </summary>
        /// <returns></returns>
        public static void StopKafkaZookeeperServer()
        {
            Process process;

            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/kafka-manage", "stop")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd1 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start----------1-------------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd1);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/kafka", "stop")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd2 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start------------2-----------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd2);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/zookeeper", "stop")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd3 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start------------3-----------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd3);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(); 
        }


        /// <summary>
        ///  启动Kafka和Zookeeper服务
        /// </summary>
        /// <returns></returns>
        public static void StartKafkaZookeeperServer()
        {
            Process process;

            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/zookeeper", "start")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd1 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start----------1-------------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd1);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            Thread.Sleep(5000);
            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/kafka", "start")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd2 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start------------2-----------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd2);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Thread.Sleep(5000);
            process = new Process
            {
                StartInfo = new ProcessStartInfo("/etc/init.d/kafka-manage", "start")
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd3 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start------------3-----------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd3);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }


        public static void RunCmd(string cmd,string param)
        {

            Process process;

            process = new Process
            {
                StartInfo = new ProcessStartInfo(cmd, param)
            };
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            var cmd1 = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Dispose();

            Console.WriteLine("bxl------------------------start----------1-------------" + DateTime.Now.ToLongTimeString());
            Console.WriteLine(cmd1);
            Console.WriteLine("bxl------------------------end-----------------------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

        }


        public static string LinuxCommand(string commandText)
        {
            Process process = new Process();
            process.StartInfo.FileName = "bash";
            process.StartInfo.CreateNoWindow = false; // 获取或设置指示是否在新窗口中启动该进程的值（不想弹出powershell窗口看执行过程的话，就=true）
            process.StartInfo.RedirectStandardInput = true;  //指示应用程序是否从StandardInput流中读取  
            process.StartInfo.RedirectStandardOutput = true; //将应用程序的输入写入到StandardOutput流中  
            process.StartInfo.RedirectStandardError = true;  //将应用程序的错误输出写入到StandarError流中  
            process.StartInfo.UseShellExecute = false;
            string strOutput = null;
            try
            {
                process.Start();
                process.StandardInput.WriteLine(commandText);
                process.StandardInput.WriteLine("exit");         //将 exit 命令写入StandardInput流中 
                strOutput = process.StandardOutput.ReadToEnd();   //读取所有输出的流的所有字符  
                process.WaitForExit();
                process.Close();
            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }
            return strOutput;
        }
    } 
}
